package org.grey.edz.converter;

import org.grey.edz.ConverterEnum;
import org.grey.edz.converter.i.IDZipConverter;
import org.grey.edz.payload.EDZPayload;
import org.grey.edz.util.EDZAssert;
import org.grey.edz.util.EDZReflectUtil;
import org.grey.edz.util.EDZUtil;

import java.lang.reflect.InvocationTargetException;
import java.util.function.Supplier;

/**
 * @program: easy-data-zipper
 * @ClassName DateConverter
 * @description: 日期转换器
 * @author: Grey
 * @create: 2024-02-19 15:38
 */
public class DateConverter implements IDZipConverter {
    public final static IDZipConverter self = new DateConverter();
    private DateConverter(){}

    @Override
    public void convert(EDZPayload payload) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        Object source = payload.getSource();
        Object target = EDZUtil.deepCopy(source);
        payload.setTarget(target);

        String[] properties = payload.getProperties();

        for (String property : properties) {
            //Object sourceVal = EDZReflectUtil.method(payload.getClazz(), EDZReflectUtil.getter(property)).invoke(source);
            Supplier<?> supplier = EDZAssert.getOrElse(payload.getValSupplier().containsKey(property)
                    , ()->ConverterEnum.DATE.supplier
                    , ()->payload.getValSupplier().get(property));
            EDZReflectUtil.method(payload.getClazz(), EDZReflectUtil.setter(property)).invoke(target,supplier.get());
        }
    }

    @Override
    public void reversal(EDZPayload payload) {


    }
}
